//
//  TKIMLocationMsgView.m
//  TKIM
//
//  Created by liupm on 15/10/27.
//  Copyright © 2015年 liupm. All rights reserved.
//

#import "TKIMLocationMsgView.h"
//#import "TKIMLocationMessage.h"
//#import "TKIMUIHelper.h"
#import "TKIMLocationViewController.h"
//#import "TKIMLocationHelper.h"

@interface TKIMLocationMsgView()
@property(nonatomic,strong) UIImageView *locationImageView;
@property(nonatomic,strong) TKIMLocationMessage *locationMsg;
@property(nonatomic,strong) TKIMLocationHelper *locationHelper;
@property(nonatomic,strong) MKMapView *mapView;
@property(nonatomic,strong) UILabel *tipLabel;
@property(nonatomic,strong) CALayer *maskLayer;

@end

@implementation TKIMLocationMsgView

/**
 *  @Author lpm, 15-07-30 11:07:12
 *
 *  @brief  组装界面
 */
-(void)drawView
{
    [super drawView];
    [self.backgroundImageView addSubview:self.locationImageView];
    _locationHelper = [[TKIMLocationHelper alloc] init];
    [self.locationImageView addSubview:self.tipLabel];
}


-(UIImageView *)locationImageView
{
    if (!_locationImageView)
    {
        _locationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, TKIMScreenWidth*0.5, 110)];
        _locationImageView.image = TKIMGetImage(@"im_bg_location");
    }
    return _locationImageView;
}


-(UILabel *)tipLabel
{
    if (!_tipLabel)
    {
        _tipLabel = [[UILabel alloc] initWithFrame:CGRectMake( 0, self.locationImageView.height-30,self.locationImageView.width, 30)];
        _tipLabel.backgroundColor = [UIColor blackColor];
        _tipLabel.font = [UIFont systemFontOfSize:12.0f];
        _tipLabel.textColor = [UIColor whiteColor];
        _tipLabel.numberOfLines = 2;
        _tipLabel.alpha = 0.5;
        _tipLabel.textAlignment = NSTextAlignmentCenter;
        _tipLabel.lineBreakMode = NSLineBreakByCharWrapping;
    }
    return _tipLabel;
}


-(CALayer *)maskLayer
{
    if (!_maskLayer)
    {
        _maskLayer = [[CALayer alloc] init];
        _maskLayer.contentsCenter = CGRectMake(0.25, 0.7, 0.1, 0.1);
        _maskLayer.contentsScale = [UIScreen mainScreen].scale;
    }
    return _maskLayer;
}


/**
 *  @Author lpm, 15-07-27 17:07:14
 *
 *  @brief  设置文字
 *
 *  @param text
 */
-(void)setMessage:(TKIMMessage *)message
{
    [super setMessage:message];
    
    if ([self.maskLayer superlayer])
    {
        [self.maskLayer removeFromSuperlayer];
    }
    
    _locationMsg = (TKIMLocationMessage *)message;
    //图片大小
    CGSize size = CGSizeMake(TKIMScreenWidth*0.5, 110);
    //设置消息label的大小
    CGRect msgRect = [self getMsgFrame:message size:size];
    msgRect = CGRectMake(0, 0, self.backgroundImageView.width, self.backgroundImageView.height);
    self.locationImageView.frame = msgRect;
    
    NSString *imgName = TKIMChatMsgReceiverImage;
    if (message.isSender)
    {
        imgName = TKIMChatMsgSenderImage;
    }
    self.tipLabel.text = _locationMsg.address;
    self.tipLabel.frame =  CGRectMake( 0, self.locationImageView.bottom-30,self.locationImageView.width, 30);
    
    UIImage *image = TKIMGetImage(imgName);
    self.maskLayer.contents = (id)image.CGImage;
    self.maskLayer.frame = (CGRect){ {0, 0}, self.backgroundImageView.frame.size };
    self.locationImageView.layer.mask = self.maskLayer;
    
    UITapGestureRecognizer *toLocation = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toLocation:)];
    self.backgroundImageView.userInteractionEnabled = YES;
    [self.backgroundImageView addGestureRecognizer:toLocation];
}


/**
 *  @Author lpm, 15-10-27 16:10:34
 *
 *  @brief  跳转到地图
 *
 *  @param tap
 */
-(void)toLocation:(UIGestureRecognizer *)tap
{
    [self routerEventWithName:TKIMLocationClickEvent userInfo:_locationMsg];
}


/**
 *  @Author lpm, 15-11-05 09:11:46
 *
 *  @brief  行高
 *
 *  @return
 */
+(CGFloat)heightForRow:(TKIMMessage *)msg
{
    //图片大小
    CGSize size = CGRectMake(0, 0, TKIMScreenWidth*0.5, 110).size;
    //消息容器大小
    return   size.height + TKIMChatMsgPadding+14;
}


@end
